﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponentsDx.Base.Components;
using System.ComponentModel;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System.Drawing;
using System.Windows.Forms;

namespace BuildHelper.Security
{
    public class SecurityProvider : BaseExtenderProvider<SecuritySettings>
    {
        private int lockImgSize = 14;
        private int lockImageAddSize = 5;

        public SecurityProvider()
        {

        }
        public SecurityProvider(IContainer components)
            : base(components)
        {

        }
        public override bool CanExtend(object extendee)
        {
            return extendee is SimpleButton ||
                extendee is BarItem;
        }
        protected override void ProcessObject(object target, SecuritySettings settings)
        {
            if (target is SimpleButton)
            {
                (target as SimpleButton).Paint -= new System.Windows.Forms.PaintEventHandler(SecurityProvider_Paint);
                (target as SimpleButton).Paint += new System.Windows.Forms.PaintEventHandler(SecurityProvider_Paint);
            }
            else if (target is BarItem)
            {
                BarItem bi = (BarItem)target;
                settings.OryginalGlyph = bi.Glyph;
                Image img = bi.Glyph;
                if (img == null)
                {
                    if (bi.Manager != null)
                    {
                        img = DevExpress.Utils.ImageCollection.GetImageListImage(bi.Manager.Images, bi.ImageIndex);
                        if (img != null)
                        {
                            Size s = DevExpress.Utils.ImageCollection.GetImageListSize(bi.Manager.Images);
                            img = img.ResizeImage(s.Width, s.Height);
                        }
                    }
                }
                if (img == null)
                {
                    Bitmap bmp = new Bitmap(16, 16);
                    bmp.MakeTransparent(bmp.GetPixel(0, 0));
                    img = bmp;
                }
                settings.LockedGlyph = AddLockIcon(img);
            }
        }
        private void RefreshLockState(object target, SecuritySettings settings)
        {
            if (target is SimpleButton)
            {
                SimpleButton btn = ((SimpleButton)target);
                btn.Enabled = !settings.Locked;
            }
            else if (target is BarItem)
            {
                BarItem bi = (BarItem)target;

                if (settings.Locked)
                    bi.Glyph = settings.LockedGlyph;
                else
                    bi.Glyph = settings.OryginalGlyph;
                if (!(bi is BarSubItem))
                    bi.Enabled = !settings.Locked;
            }
        }

        public void SetLocked(object target, bool locked)
        {
            object key = GetRealKey(target);
            if (ContainsSettingsFor(key))
            {
                var settings = GetSettings(key);
                if (settings.Locked != locked)
                {
                    settings.Locked = locked;
                    RefreshLockState(target, settings);
                }
            }
        }
        public bool IsLocked(object target)
        {
            object key = GetRealKey(target);
            if (ContainsSettingsFor(key))
                return GetSettings(key).Locked;
            return false;
        }
        private Image AddLockIcon(Image image)
        {
            Image copy = null;
            if (image != null)
            {
                copy = new Bitmap(image.Width + lockImageAddSize, image.Height);
                Graphics graphicsDisabled = Graphics.FromImage(copy);
                graphicsDisabled.DrawImage(image, 0, 0);
                int x = image.Width - lockImgSize + lockImageAddSize;
                int y = image.Height - lockImgSize;

                graphicsDisabled.DrawImage(Properties.Resources.gear, x, y, lockImgSize, lockImgSize);
            }
            return copy;
        }

        void SecurityProvider_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            object key = GetRealKey(sender);
            Control control = sender as Control;
            if (control != null)
            {
                if (ContainsSettingsFor(key))
                {
                    var settings = GetSettings(key);
                    if (settings.Locked)
                    {
                        int x = control.Width - lockImgSize;
                        int y = control.Height - lockImgSize;

                        e.Graphics.DrawImage(Properties.Resources.gear, x, y, lockImgSize, lockImgSize);
                    }
                }
            }
        }

        protected override SecuritySettings CreateSettings(object target)
        {
            return new SecuritySettings(target, this);
        }
    }
}
